﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Open_MUD.Mudlib.lib
{
    class MUDObject
    {
        private string uid;
		protected bool reloadshort = false;
		protected bool reloadlong = false;		
		
		public enum Genero{Masculino,Femenino};
		
		private ArrayList alias = new ArrayList();
		public void AddAlias(string alias)
		{
			if (! this.alias.Contains(alias) )
				this.alias.Add(alias);
		}		
		public void RemoveAlias(string alias)
		{
			if ( this.alias.Contains(alias) )
				this.alias.Remove(alias);
		}
		
		private string nombre="";		
		public string Nombre
		{
			get { return nombre; }
			set {if (nombre.Equals("") ) this.nombre = value;}
		}
		
		private string shortplural="";
		private string shortdesc="";		
		public string Short
		{
			get {
			   if ( reloadshort )
			      shortdesc =  recalcShort();			      
			   else
			   if ( shortdesc.Equals("") ) 
			   {
			      shortdesc = nombre;			      
			   } 
				return shortdesc; }			
			set {shortdesc = value;
			     shortplural = value;}
		}
		
		public string ShortPlural
		{
		   get{
		      if (reloadshort)
		         shortplural = recalcPlural();
		      else
		      if ( shortplural.Equals(""))
		      {
		         shortplural = nombre;
		      }
		      return shortplural;
		   }
		   set{
		      shortplural = value;
		   }		
		}
		
		private string longdesc="";		
		public string Long
		{
			get { 
			   if ( reloadlong )
			      longdesc = recalcLong();
			   return longdesc; 
			}			
			set {longdesc= value;}
		}
		
		private Genero sexo = Genero.Masculino;
		public Genero Sexo{
		   get {return sexo;}
		   set {sexo = value;}
		}
		
		public void RestoreShort()
		{
			shortdesc = "";
		}
		
		protected virtual string recalcShort(){return shortdesc;}
		protected virtual string recalcLong(){ return longdesc;}
        protected virtual string recalcPlural() { return shortplural; }
		
		public bool Match(String pattern)
		{
			if ( nombre.Contains(pattern) )
				return true;
			int i = 0;
			while ( i < alias.Count )
			{
				if ( (alias[i] as string).Contains(pattern) )
					return true;
				i++;
			}
			return false;
		}
		
		public bool MatchExact(String pattern)
		{
			if ( nombre.Equals(pattern) )
				return true;
			int i = 0;
			while ( i < alias.Count )
			{
				if ( (alias[i] as string).Equals(pattern) )
					return true;
				i++;
			}
			return false;
		}
		
        public MUDObject()
        {
            uid = System.Guid.NewGuid().ToString();
        }
        
        public virtual string toXML(string inner)
        {
           String aliases="";
           for(int i =  0  ; i < alias.Count ; i++)
              aliases +="<alias>"+alias[i]+"</alias>";
           return "<mudobject>"+
                   "<uid>"+uid+"</uid>"+
                   "<nombre>"+nombre+"</nombre>"+
                   "<short>"+shortdesc+"</short>"+
                   "<long>"+longdesc+"</long>"+
                   "<aliases>"+aliases + "</aliases>"+
                  inner + 
                  "</mudobject>";
        }
        
        public virtual string getPath()
        {
           return "/mudobject";
        }
        
        public virtual void fromXML()
        {
           
        }
        
        public void readXML(string path)
        {
           
        }
        
        public void writeXML(string path)
        {
           String content = this.toXML("");
        }
        
    }
}
